iT邦幫忙

2025 iThome 鐵人賽

DAY 21
0
Security

一天一題picoCTF:從Easy開始的新手生活系列 第 21

Day 21 - command injection

  • 分享至 

  • xImage
  •  

Command Injection(命令注入)是 Web 題常見的漏洞類型之一:當應用把使用者輸入拼接到系統命令並執行,攻擊者就能插入額外指令讓伺服器執行未授權的命令、讀寫檔案或取得 shell。

前情提要:若你還沒熟悉基本 Shell 指令,建議先複習基本 Shell 概念,會讓下面的例子更容易理解喔~

什麼是 Command Injection?

  • 應用直接把使用者輸入字串拼進系統命令(例如 system("ping " + user_input))且未妥善過濾或限制
  • 使用 exec, system, popen, os.system(Python)、Runtime.exec(Java)等 API,並把未驗證/未過濾的參數傳入。
  • 伺服器使用 shell 解析整個字串(而非直接呼叫 execve 類的系統介面),會允許指令串接與特殊字元擴張。

常見類型

1. 直接注入

使用者輸入直接跟在命令後面:os.system("ping -c 1 " + user_input)

user_input = "example.com; cat /etc/passwd" → 會執行 ping -c 1 example.com; cat /etc/passwd

2. 盲注(Blind command injection)

輸出不回顯到網頁,但可以從行為差異(回應時間、響應長度、狀態碼)推斷命令執行結果。常用 sleepping 製造延時作為訊號。

例:example.com && sleep 5 → 若回應延遲表示命令被執行。

3. Shell Metacharacter Bypass / Encoding Tricks

應用會過濾某些字元(例如 ;&|),但可利用其它分隔符或命令替代(反引號、$()||&&>$(echo ... | sh)、環境變數展開等)或用 URL encode/hex encode/重複編碼繞過。

4. OS-specific 注入(Windows vs Unix)

Windows 常用 &|^cmd /c、PowerShell 表達。Payload 需依作業系統調整。

常見快速 payload(Unix / Windows)

測試請在合法環境(自己的 lab / CTF 平台)操作,切勿攻擊未授權系統。

Unix / Linux 範例

  • 基本測錯(單引號/雙引號/分號):

    ; id
    `id`
    $(id)
    && id
    || id
    
  • 取得系統資訊:

    ; whoami
    ; id
    
  • 讀檔案:

    ; cat /etc/passwd
    ; head -n 20 /var/www/html/config.php
    
  • 盲注(時間訊號):

    ; sleep 5
    && sleep 5
    `sleep 5`
    

Windows 範例

  • 基本:

    & whoami
    & type C:\Windows\System32\drivers\etc\hosts
    
  • PowerShell:

    ; powershell -c "Get-ChildItem C:\"
    

picoCTF - caas

https://ithelp.ithome.com.tw/upload/images/20250916/201691053hmYrEOHVm.png

打開網頁可以看到他叫我們make a request to the URL,在最後打上我們要的訊息

先隨便打個hello,可以看到出現了一隻牛,上面放了我們的訊息

https://ithelp.ithome.com.tw/upload/images/20250916/20169105Ug5XGZ9mqm.png

加個;ls來測試有沒有command injection

https://ithelp.ithome.com.tw/upload/images/20250916/20169105JEguxEMJOB.png

好欸有漏洞,可以看到一個叫falg.txt的,感覺就很像flag,用cat 指令看看

https://ithelp.ithome.com.tw/upload/images/20250916/20169105nYbSLziYMj.png

好欸這樣就獲得flag啦~


以上就是今天的內容啦

想看更多,記得明天再來喔~


上一篇
Day 20 - Path Traversal
下一篇
Day 22 - File Upload
系列文
一天一題picoCTF:從Easy開始的新手生活27
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言